﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DefeatEpidemic.Common
{
    public class Tools
    {
        /// <summary>
        /// 生成指定长度的随机数字
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetRandomNum(int length)
        {
            var result = new System.Text.StringBuilder();
            for(var i = 0; i < length; i++)
            {
                var r = new Random(Guid.NewGuid().GetHashCode());
                result.Append(r.Next(0, 10));
            }
            return result.ToString();
        }

        public static String[] chars36 = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        /// <summary>
        /// 生成指定长度唯一标识码
        /// </summary>
        /// <returns></returns>
        public static String GenerateInviteCode(int length)
        {
            System.Text.StringBuilder shortBuffer = new System.Text.StringBuilder();
            String uuid = Guid.NewGuid().ToString("N");
            for (int i = 0; i < length; i++)
            {
                String str = uuid.Substring(i * 4, 4);
                long x = Convert.ToInt64(str, 16);
                shortBuffer.Append(chars36[x % 36]);
            }
            return shortBuffer.ToString();
        }
    }
}
